import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from '@/utils/auth';
Vue.use(Router)
// 首页
import Index from '@/view/index/index.vue';
import Resume from '@/view/resume/resume.vue';
import Project from '@/view/project/project.vue';
import BlogArticle from '@/view/blogArticle/blogArticle.vue';
import Article from '@/view/article/article.vue';
import Login from '@/view/login.vue';
import { Message } from "element-ui";
const router = new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
    { path: '/', redirect: '/index' },
    { path: '/index', name: 'index', component: Index },
    { path: '/resume', name: 'resume', component: Resume },
    { path: '/project', name: 'project', component: Project },
    { path: '/blogArticle', name: 'blogArticle', component: BlogArticle },
    { path: '/article/:id', name: 'article', component: Article },
    { path: '/login', name: 'login', component: Login },
  ]
});

// 解决导航栏中的Vue-router在3.0版本以上重点菜单报错问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err);
};

router.beforeEach(async (to, from, next) => {
  const token = getToken()
  if (!token && to.path !== '/login'){
    next('/login')
    return;
  }
  next();
});
router.afterEach(() => {
  window.scrollTo(0, 0);
});
export default router;

// export const subRouter = _subRouter;
